package com.chixing.day14_map;


import java.util.*;

public class FooTest {
    public static void main(String[] args) {
        Foo f1=  new Foo("测试1","test a") ;
        Foo f2= new Foo("测试2","test b")  ;
        Foo f3= new Foo("测试1","test c");
        Foo f4= new Foo("测试3","test d");
        Foo f5= new Foo("测试3","test e") ;
        Foo f6= new Foo("测试4","test f");
        List<Foo> list =  new ArrayList<>();
        list.add(f1);
        list.add(f2);
        list.add(f3);
        list.add(f4);
        list.add(f5);
        list.add(f6);
        System.out.println(list);


         /*将以上List转换成Map
    Map{
        "测试1", List(Foo("测试1","test a"),Foo("测试1","test c")),
                "测试2", List(Foo("测试2","test b")),
                "测试3", List(Foo("测试3","test d"),Foo("测试3","test e")),
                "测试4", List(Foo("测试4","test f"))
    }*/
        Map<String,List<Foo>> fooMap = new HashMap<>();
        for (Foo foo : list){
            String code = foo.getCode();  //获得code
            if (fooMap.containsKey(code)){
                List<Foo> fooList = fooMap.get(code);
                fooList.add(foo);

            }else {
                List<Foo> fooList = new ArrayList<>();
                fooList.add(foo);
                fooMap.put(code,fooList);
            }
            System.out.println(fooMap);
        }

    }


}
